package com.uduew.sjd.request;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

import com.uduew.sjd.api.RepaymentLinkApi;
import com.uduew.sjd.bean.HttpBean;
import com.uduew.sjd.net.DataRepository;
import com.uduew.sjd.net.DataResult;
import com.kunminx.architecture.domain.message.MutableResult;
import com.kunminx.architecture.domain.message.Result;

import org.jetbrains.annotations.NotNull;

import io.reactivex.disposables.Disposable;

public class RepaymentLinkRequester extends Requester implements DefaultLifecycleObserver {

    private final MutableResult<DataResult<HttpBean<RepaymentLinkApi.ResRePaymentLinkBean>>> result = new MutableResult<>();

    public Result<DataResult<HttpBean<RepaymentLinkApi.ResRePaymentLinkBean>>> getResult() {
        return result;
    }

    private Disposable mDisposable;

    public void request(LifecycleOwner lifecycleOwner, RepaymentLinkApi.ReqRePaymentLinkBean bean) {
        DataRepository.getInstance().repaymentLink(lifecycleOwner, new RepaymentLinkApi(bean), result::postValue);
    }

    public void cancelLogin() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
            mDisposable = null;
        }
    }

    @Override
    public void onStop(@NonNull @NotNull LifecycleOwner owner) {
        cancelLogin();
    }
}
